package org.hxyjs.service.impl;

import org.hxyjs.dao.NewsRecordDao;
import org.hxyjs.dto.DTO;
import org.hxyjs.dto.MessageEnum;
import org.hxyjs.entity.NewsRecord;
import org.hxyjs.page.PageInfo;
import org.hxyjs.service.NewsRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service("NewsRecordService")
public class NewsRecordServiceImpl implements NewsRecordService {
    @Resource
    NewsRecordDao newsRecordDao;
    @Override
    public DTO<NewsRecord> getAllNewsRecords()  throws Exception{
        List<NewsRecord> newsRecords = newsRecordDao.getAllNewsRecords();
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(newsRecords).isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
        return new DTO<NewsRecord>(messageEnum.getCode(),messageEnum.getMessage(),newsRecords);
    }

    @Override
    public DTO<NewsRecord> deleteNewsRecordByNewno(String newno)  throws Exception{
        Integer result = newsRecordDao.deleteNewsRecordByNewno(newno);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  =result> 0?MessageEnum.DELETEADMINSUCCESS:MessageEnum.DELETEADMINFAIL;
        return new DTO<NewsRecord>(messageEnum.getCode(),messageEnum.getMessage());
    }
    @Override
    public DTO<NewsRecord> getNewsRecordByNewno(String newno)  throws Exception{
        NewsRecord newsRecords = newsRecordDao.getNewsRecordByNewno(newno);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(newsRecords).isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
        return new DTO<NewsRecord>(messageEnum.getCode(),messageEnum.getMessage(),newsRecords);
    }

    @Override
    public DTO<NewsRecord> updateNewsRecordByNewno(NewsRecord newsRecord) throws Exception {
        Integer result = newsRecordDao.updateNewsRecordByNewno(newsRecord);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  =result> 0?MessageEnum.UPDATEADMINSUCCESS:MessageEnum.UPDATEADMINFAIL;
        return new DTO<NewsRecord>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<NewsRecord> addNewsRecord(NewsRecord newsRecord) throws Exception {
        Integer result = newsRecordDao.addNewsRecord(newsRecord);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  =result> 0?MessageEnum.ADDADMINSUCCESS:MessageEnum.ADDADMINFAIL;
        return new DTO<NewsRecord>(messageEnum.getCode(),messageEnum.getMessage());
    }

    /**
     * 学生选课成功 学生成绩加1分，从选课信息中可以获得学生的学号就能查询出学生原来的成绩
     * @param
     * @return
     * @throws Exception
     */
    @Override
    public DTO<NewsRecord> getNewsRecordByPage(PageInfo pageInfo) throws Exception {
        List<NewsRecord> newsRecords = newsRecordDao.getNewsRecordByPage(pageInfo);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(newsRecords).isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
        return new DTO<NewsRecord>(messageEnum.getCode(),messageEnum.getMessage(),newsRecords,pageInfo);
    }

}
